package com.hg.oa.module.home.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hg.oa.R;
import com.hg.oa.module.home.layer.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import rx.fxa.com.carousel.CarouselFragment;
import rx.fxa.com.carousel.ImageItem;
import rx.fxa.com.carousel.OnItemClickListener;

/**
 * Created by fengxiang on 2016/9/20.
 */
public class HomeRecycleViewAdapter extends RecyclerView.Adapter<HomeRecycleViewAdapter.Holder> {

    String tag = "HomeRecycleViewAdapter";
    HomeFragment homeFragment;
    Context context;

    static int type_header_logo = 1;
    static int type_header_menu = 2;
    static int type_item_notice = 3;
    static int type_item_qiandao = 4;

    class Node {
        public Node(int type) {
            this.type = type;
        }

        public int type; //item 类型
    }

    List<Node> data;

    public HomeRecycleViewAdapter(HomeFragment homeFragment) {
        this.homeFragment = homeFragment;
        this.context = homeFragment.getActivity();
        data = new ArrayList<>();
        data.add(new Node(type_header_logo));
        data.add(new Node(type_header_menu));
        data.add(new Node(type_item_notice));
        data.add(new Node(type_item_notice));
        data.add(new Node(type_item_notice));
        data.add(new Node(type_item_notice));
        data.add(new Node(type_item_notice));
        data.add(new Node(type_item_notice));
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        Holder holder = null;
        if (viewType == type_header_logo) {
            view = LayoutInflater.from(context).inflate(R.layout.item_home_header_logo, parent, false);
            holder = new HolderLogo(view);
        } else if (viewType == type_header_menu) {
            view = LayoutInflater.from(context).inflate(R.layout.item_home_header_menu, parent, false);
            holder = new HolderMenu(view);
        } else if (viewType == type_item_qiandao) {
            view = LayoutInflater.from(context).inflate(R.layout.item_home_has_qiandao, parent, false);
            holder = new HolderQiandao(view);
        } else if (viewType == type_item_notice) {
            view = LayoutInflater.from(context).inflate(R.layout.item_home_notice, parent, false);
            holder = new HolderNotice(view);
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        Node node = data.get(position);
        return node.type;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {

    }


    public class Holder extends RecyclerView.ViewHolder {

        public Holder(View itemView) {
            super(itemView);
        }
    }

    public class HolderQiandao extends Holder {

        public HolderQiandao(View itemView) {
            super(itemView);
        }
    }

    public class HolderNotice extends Holder {

        public HolderNotice(View itemView) {
            super(itemView);
        }
    }


    String url1 = "http://tupian.enterdesk.com/2013/xll/009/07/7/1.jpg";
    String url2 = "http://tupian.enterdesk.com/2013/xll/0110/nuansemeijing/nuanse%20(12).jpg";
    String url3 = "http://tupian.enterdesk.com/2013/mxy/0307/8/1.jpg";
    String url4 = "http://d.hiphotos.baidu.com/zhidao/pic/item/3b87e950352ac65c1b6a0042f9f2b21193138a97.jpg";

    public class HolderLogo extends Holder {

        public HolderLogo(View itemView) {
            super(itemView);
            CarouselFragment carouselFragment = new CarouselFragment();
            FragmentManager fragmentManager = homeFragment.getChildFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.fr_carousel, carouselFragment);
            transaction.commit();

            ArrayList<ImageItem> items = new ArrayList<>();
            items.add(new ImageItem(url1, null));
            items.add(new ImageItem(url2, null));
            items.add(new ImageItem(url3, null));
            items.add(new ImageItem(url4, null));
            carouselFragment.setData(items);
            carouselFragment.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(ImageItem imageItem) {
                    Log.e(tag, imageItem.toString());
                }
            });
        }
    }

    public class HolderMenu extends Holder implements View.OnClickListener {

        TextView text_qiandao;
        TextView text_input_dayreport;
        TextView text_input_level;
        TextView text_chart;

        public HolderMenu(View itemView) {
            super(itemView);
            text_qiandao = (TextView) itemView.findViewById(R.id.text_qiandao);
            text_input_dayreport = (TextView) itemView.findViewById(R.id.text_input_dayreport);
            text_input_level = (TextView) itemView.findViewById(R.id.text_input_level);
            text_chart = (TextView) itemView.findViewById(R.id.text_chart);
            text_qiandao.setOnClickListener(this);
            text_input_dayreport.setOnClickListener(this);
            text_input_level.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            onMenuClickListener.onMenuClick(v);
            Log.i(tag, "onClick-->" + v.getId());
        }
    }

    private OnMenuClickListener onMenuClickListener;

    public interface OnMenuClickListener {
        void onMenuClick(View view);
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }
}
